#include "ComboBox.h"
#include <cstring>
#include "InputMgr.h"

ComboBox::ComboBox()
{
	m_hf = CreateFont(15, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");
}

ComboBox::~ComboBox()
{
	if(m_pBuffer)
	{
		delete m_pBuffer;
		m_pBuffer = NULL;
	}
}
bool ComboBox::Setup(HWND a_hWnd, HINSTANCE a_hInst, int a_xPos, int a_yPos, int a_width, int a_height, const char* a_entries[], int a_numItems)
{
	m_hWnd = CreateWindowEx(NULL, "COMBOBOX", "", CBS_DROPDOWNLIST | CBS_DISABLENOSCROLL | CBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_CHILD | WS_VISIBLE | WS_TABSTOP, a_xPos, a_yPos, a_width, a_height, a_hWnd, NULL, a_hInst, NULL);
	if(m_hWnd == NULL)
	{
		return false;
	}
	for(int i=0; i<a_numItems; ++i)
	{
		SendMessage(m_hWnd, CB_ADDSTRING, 0, (LPARAM)a_entries[i]);
	}
	//Clear Selection
	SendMessage(m_hWnd, CB_SETCURSEL, (WPARAM)-1, 0);

	SendMessage(m_hWnd, WM_SETFONT, (WPARAM)m_hf, (LPARAM)true);	
	m_pBuffer = new char[512];

	return true;
}
bool ComboBox::IsSelected(const char* a_pSelection)
{
	//Check if the combo box is in the dropped down state, if its not, do not keep checking
	if(SendMessage(m_hWnd, CB_GETDROPPEDSTATE, 0, 0))
	{
		//The selection is updated on mouse over, instead of mouse press, so this semi hack is to help
		//NOTE: This still does not work as the function will still return if the user presses the mouse button anywhere in the window while selected is equal
		if(InputMgr::GetInstance().IsMouseButtonPressed(InputMgr::MouseButton_Left))
		{
			//Fucking stupid, returns 0 when equal.
			if(strcmp(GetSelection(), a_pSelection) == 0)
			{
				return true;
			}
		}
	}
	//Incorrect match or not selected yet
	return false;
}
void ComboBox::SetSelection(int a_index)
{
	ComboBox_SetCurSel(m_hWnd, a_index);
}
